package com.hzit.security.handler;

import com.alibaba.fastjson.JSON;
import com.hzit.util.R;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 类名：
 * 作者：WF
 * 功能：处理授权异常的类 ( 适用于前后端分离 )：403异常（无权访问）
 */
@Component
public class HzitAccessDeniedHandler implements AccessDeniedHandler {
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
		response.setContentType("text/html;charset=utf8");
		R<Object> fail = R.fail("没有访问资源的权限！");
		String s = JSON.toJSONString(fail);
		response.getWriter().println(s);
		response.getWriter().flush();
	}
}
